﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Timers;

namespace Campus {
    class MeleeAttack : AttackAbility {
        private int m_range = 15;
        public MeleeAttack(BaseEntity owner, Animation animation, TargetTypes targetType, 
            int minDamage, int maxDamage, int attackInterval)
            : base(MenuIcons.AttackIcon, owner, animation, targetType, minDamage, maxDamage, attackInterval) {
            LastAttackTime = 0;
        }
        public MeleeAttack(MeleeAttack copySource)
            : base(copySource) {
        }
        public override Ability Clone() {
            return new MeleeAttack(this);
        }
        public override void Update(GameTime gameTime, bool checkInput, bool updateBoundingBox) {
            if (!IsTargetValid()) {
                IsAbilityDone = true;
                Owner.IsAttacking = false;
                return;
            }
            
            if (Target is BaseEntity) {
                BaseEntity target = (BaseEntity)Target;
                BoundingSphere rangeSphere = new BoundingSphere(Owner.BoundingSphere.Center, Owner.BoundingSphere.Radius + m_range);
                if (target.BoundingSphere.Intersects(rangeSphere)) {
                    //TODO: Add case if owner isnt unit
                    Unit unit = (Unit)Owner;
                    Owner.IsAttacking = true;
                    LastAttackTime += gameTime.ElapsedRealTime.Milliseconds;
                    if (AttackInterval <= LastAttackTime) {
                        target.TakeDamage(new Random().Next(MinimumDamage, MaximumDamage + 1));
                        //TODO: do animation if not null
                        if (Owner.CurrentAnimation != null)
                        {
                            var unitOwner = Owner as Unit;

                            if (unitOwner != null && unitOwner.CurrentState != Unit.UnitState.Die && 
                                !Owner.CurrentAnimation.WillLoop)
                            {
                                Owner.CurrentAnimation.ResetAnimation();
                                Owner.CurrentAnimation.Start();
                            }
                        }
                        LastAttackTime = 0;
                    }
                } else {
                    //move to the target if it is out of range
                    if (Owner is Unit) {
                        Unit unit = (Unit)Owner;
                        MoveAbility move = (MoveAbility)unit.GetMoveAbility().Clone();
                        move.FinalDestination = ((BaseEntity)Target).GetCenter();
                        move.StopDelegate = delegate() {
                            return target.BoundingSphere.Intersects(Owner.BoundingSphere);
                        };
                        unit.FrontQueueAbility(move);
                        unit.IsAttacking = false;
                    } else {
                        //structure, so do nothing
                        Target = null;
                    }
                }
            }
        }
    }
}
